package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.sql.PreparedStatement;

import common.Constant;

import model.User;

import util.Md5encode;
import util.ParameterCheck;

public class UserDao extends RpDao {
	
		

	public UserDao() {
		super();
	}
	

	public int reg(String email, String pwd, String name) {
		int s;// 返回值
		Connection conn=this.getConn();
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(SqlString.selectUser);
			ps.setString(1, email);
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				s = 0;// 已经存在的用户
			} else {
				ps = conn.prepareStatement(SqlString.selectUserbyName);
				ps.setString(1, name);
				rs = ps.executeQuery();

				if (rs.next()) {
					s = -1;
				} else {

					// insertUser="insert into user(email,password,name)values(?,?,?);",
					ps = conn.prepareStatement(SqlString.insertUser,
							Statement.RETURN_GENERATED_KEYS);
					ps.setString(1, email);
					ps.setString(2, Md5encode.getMD5ofStr(pwd));
					ps.setString(3, name);
					int n = ps.executeUpdate();
					if (1 == n) {
						rs = ps.getGeneratedKeys();
						rs.next();
						int uid = rs.getInt(1);
						s = uid;// 成功则返回 userid
						rs.close();
					} else
						s = -2;// insert 失败
				}
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			s = -3;// sqlerror
		}finally{
			this.closeDao(conn, ps, null);
		}
		
		return s;
	}

	public int login(String email, String pwd) {
		// >0：用户id，-403：密码错误 -404：用户不存在 -500:系统错误
		int id = 0;
		Connection conn=this.getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			// select
			// id,name,email,password,sex,birthday,sign,regdate,lastlogin,level,pic,score,qq
			// from user where email=?;
			ps = conn.prepareStatement(SqlString.selectUser);
			ps.setString(1, email);
			rs = ps.executeQuery();

			if (rs.next()) {
				String userpwd = rs.getString("password");

				if (Md5encode.getMD5ofStr(pwd).equals(userpwd))
					id = rs.getInt("id");
				else
					id = -403;

			} else
				id = -404;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			id = -500;
		}
finally{
	this.closeDao(conn, ps, rs);
}
		return id;

	}

	public User getUser(int uid) {
		User user = null;
		Connection conn=this.getConn();
		
		ResultSet rs =null;
		try {
			// selectUserbyid="select
			// id,name,email,password,sex,birthday,sign,regdate,lastlogin,level,pic,score,qq
			// from user where id=
			rs = conn.createStatement().executeQuery(SqlString.selectUserbyid + uid);
			if (rs.next()) {
				user = new User();
				user.setId(rs.getInt("id"));
				user.setBirthday(rs.getString("birthday"));
				user.setName(rs.getString("name"));
				user.setSex(rs.getInt("gender"));
				user.setSign(rs.getString("sign"));
				user.setRegdate(rs.getString("ctime"));
				user.setLastlogin(rs.getString("lastlogin"));
				user.setEmail(rs.getString("email"));
				user.setLevel(rs.getInt("level"));
				user.setQq(rs.getString("qq"));
				user.setPic(Constant.baseurl
						+ ParameterCheck.checkNull(rs.getString("img"),
								"/user/img/default.jpg"));
				user.setCity(rs.getString("city"));
			}
			// System.out.println("uid:"+user.getId());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			this.closeDao(conn, null, rs);
		}
		return user;
	}

	public int updateUserInfo(User user) {
		int n = -1;
		Connection conn=this.getConn();
		PreparedStatement ps=null;
		ResultSet rs =null;
		// updateUser="update user set name=?,sign=?,sex=?,birthday=?,qq=?,addr=? where id=?",
		try {

			ps = conn.prepareStatement(SqlString.selectUserbyName);
			ps.setString(1, user.getName());
			rs = ps.executeQuery();

			if (rs.next()) {
				if (user.getId() != rs.getInt("id"))
					n = 0;
			}
			if (n != 0) {
				ps = conn.prepareStatement(SqlString.updateUser);
				ps.setString(1, user.getName());
				ps.setString(2, user.getSign());
				ps.setInt(3, user.getSex());
				ps.setString(4, user.getBirthday());
				ps.setString(5, user.getQq());
				ps.setString(6, user.getCity());
				ps.setInt(7, user.getId());

				n = ps.executeUpdate();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			n = -1;
		}
		finally{
			this.closeDao(conn, ps, rs);
		}
		return n;
	}

	public void updateImg(int uid, String path) {

		// updateUserImg="update user set pic=? where id=?",
		Connection conn=this.getConn();
		PreparedStatement ps = null;
		try {
			ps =conn.prepareStatement(SqlString.updateUserImg);
			ps.setString(1, path);
			ps.setInt(2, uid);
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			this.closeDao(conn, ps,null);
		}

	}

	public void updatePwd(String uid, String pwd) {

		// updatePwd="update user set password=? where id=?",
		Connection conn=this.getConn();
		PreparedStatement ps = null;
		try {
			ps =conn.prepareStatement(SqlString.updatePwd);
			ps.setString(1, pwd);
			ps.setInt(2, Integer.valueOf(uid));
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			this.closeDao(conn, ps, null);
		}
	}
	
	public Set<Integer> getUidsByNames(List<String> names){
		Set<Integer> uids = new HashSet<Integer>();
		Connection conn=this.getConn();
//		selectUidsByName="select id from user where name=?",
		PreparedStatement ps = null;
		ResultSet rs=null;
		try {
			while(!names.isEmpty()){
				ps=conn.prepareStatement(SqlString.selectUidsByName);
//				System.out.println("xxxxxxxxxxx"+names.get(0));
				ps.setString(1, names.remove(0));
				rs=ps.executeQuery();
				if (rs.next()){
					uids.add(rs.getInt("id"));
				}
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			this.closeDao(conn, ps, rs);
		}
				
		return uids;
	}
		
}
